import { isEqual } from 'lodash'
import { create } from 'zustand'

interface ThemeState {
  isDark: boolean
  setTheme: (nv: boolean) => void
}

const useThemeStore = create<ThemeState>()((set, get) => ({
  isDark: false,
  setTheme: (nv: boolean) => {
    const currentIsDark = get().isDark
    if (!isEqual(nv, currentIsDark)) {
      window.electron.ipcRenderer.send('updateGlobalState', nv)
      set(() => ({ isDark: nv }))
    }
  }
}))

export default useThemeStore
